//
//  YKAddClientVisitLogController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKAddClientVisitLogController.h"
#import "YKInputView.h"
#import "YKSelectView.h"
#import "YKInputMultilineView.h"
#import "YKClientDataModel.h"
#import <IQPreviousNextView.h>
#import "YKClientVisitDetailModel.h"
@interface YKAddClientVisitLogController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong)NSString *visitno;
@property (nonatomic, strong)NSString *visitdate;
@property (nonatomic, strong)NSArray<YKClientDataModel *> *cdList;
@property (nonatomic, strong)YKSelectView* selectClientView;
@property (nonatomic, strong)YKInputMultilineView *visitPurposeView;
@property (nonatomic, strong)YKInputMultilineView *visitResultView;
@property (nonatomic, strong)YKInputView* visigPersonView;
@property (nonatomic, strong)YKInputView* contactView;
@property (nonatomic, strong)YKInputView* positionView;
@property (nonatomic, strong)YKInputView* birthdayView;
@property (nonatomic, strong)YKInputMultilineView* characterView;
@property (nonatomic, strong)YKInputMultilineView* hobbyView;
@property (nonatomic, strong)YKClientVisitDetailModel *detailModel;
@end

@implementation YKAddClientVisitLogController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"新增客户拜访";
    self.view.backgroundColor = [UIColor whiteColor];
    IQPreviousNextView *contentView = [[IQPreviousNextView alloc]init];
    [self.view addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.scrollView = [[UIScrollView alloc]init];
    [contentView addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(contentView);
        make.bottom.equalTo(contentView.mas_bottom).offset(-44);
    }];
    
    
    if (self.m_id) {
        [self tf_showStateView:kTFViewStateLoading];
        [TFHTTPMANAER POST:@"customerDataVisitLog/searchCustomerDataVisitLogById.htm" parameters:@{@"id":self.m_id} success:^(id responseObject) {
            self.detailModel = [YKClientVisitDetailModel yy_modelWithJSON:responseObject[@"customerDataVisitLog"]];
            self.cdList = [NSArray yy_modelArrayWithClass:[YKClientDataModel class] json:responseObject[@"customerDataList"]];
            [self updateView];
            [self tf_showStateView:kTFViewStateNone];
        } failure:^(id responseObject, NSError *error) {
            [self tf_showStateView:kTFViewStateNetError];
        }];
    }
    else {
        [self tf_showStateView:kTFViewStateLoading];
        [TFHTTPMANAER POST:@"customerDataVisitLog/toAddCustomerDataVisitLog.htm" parameters:@{} success:^(id responseObject) {
            self.visitno = responseObject[@"visitno"];
            self.visitdate = responseObject[@"visitdate"];
            self.detailModel = [[YKClientVisitDetailModel alloc]init];
            self.detailModel.VISITNO = self.visitno;
            self.detailModel.VISITDATE = self.visitdate;
            self.cdList = [NSArray yy_modelArrayWithClass:[YKClientDataModel class] json:responseObject[@"cdList"]];
            [self updateView];
            [self tf_showStateView:kTFViewStateNone];
        } failure:^(id responseObject, NSError *error) {
            [self tf_showStateView:kTFViewStateNetError];
        }];
    }
    
}

- (void)updateView {
    
    if (!self.m_id) {
        UIButton* saveButton = [[UIButton alloc]init];
        [self.view addSubview:saveButton];
        UIButton *cancleButton = [[UIButton alloc]init];
        [self.view addSubview:cancleButton];
        [saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        [saveButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
        [cancleButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
        [saveButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
        [cancleButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
        saveButton.titleLabel.font = [UIFont systemFontOfSize:14];
        cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [saveButton addTarget:self action:@selector(onSaveButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [cancleButton addTarget:self action:@selector(onCancleButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        
        [@[saveButton,cancleButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
        [@[saveButton,cancleButton] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view);
            make.height.mas_equalTo(44);
        }];
    }
    
    YKInputView* visigNoView = [[YKInputView alloc]initWithTitle:@"拜访单号" input:self.detailModel.VISITNO];
    
    visigNoView.editable = NO;
    [self.scrollView addSubview:visigNoView];
    
    YKInputView* visigDateView = [[YKInputView alloc]initWithTitle:@"拜访日期" input:self.detailModel.VISITDATE];
    visigDateView.editable = NO;
    
    [self.scrollView addSubview:visigDateView];
    
    YKSelectView* selectClientView = [[YKSelectView alloc]initWithTitle:@"选择客户" row:self.cdList initialText:self.cdList.firstObject];
    self.selectClientView = selectClientView;
    selectClientView.enabled = self.m_id == nil;
    [self.scrollView addSubview:selectClientView];
    
    YKInputMultilineView *visitPurposeView = [[YKInputMultilineView alloc]initWithTitle:@"拜访目的" input:self.detailModel.VISITGOAL];
    self.visitPurposeView = visitPurposeView;
    visitPurposeView.editable = self.m_id == nil;
    [self.scrollView addSubview:visitPurposeView];
    
    YKInputMultilineView *visitResultView = [[YKInputMultilineView alloc]initWithTitle:@"拜访结果" input:self.detailModel.VISITRESULT];
    self.visitResultView = visitResultView;
    visitResultView.editable = self.m_id == nil;
    [self.scrollView addSubview:visitResultView];
    
    YKInputView* visigPersonView = [[YKInputView alloc]initWithTitle:@"拜访人员" input:self.detailModel.VISITPERSON];
    self.visigPersonView = visigPersonView;
    visigPersonView.editable = self.m_id == nil;
    [self.scrollView addSubview:visigPersonView];
    
    YKInputView* contactView = [[YKInputView alloc]initWithTitle:@"联系方式" input:self.detailModel.LINKPHONE];
    self.contactView = contactView;
    contactView.editable = self.m_id == nil;
    [self.scrollView addSubview:contactView];
    
    YKInputView* positionView = [[YKInputView alloc]initWithTitle:@"职务" input:self.detailModel.JOB];
    self.positionView = positionView;
    positionView.editable = self.m_id == nil;
    [self.scrollView addSubview:positionView];
    
    YKInputView* birthdayView = [[YKInputView alloc]initWithTitle:@"生日" input:self.detailModel.BIRTHDAY];
    birthdayView.dateFormat = @"yyyy-MM-dd";
    birthdayView.type = YKInputViewTypeDate;
    self.birthdayView = birthdayView;
    birthdayView.editable = self.m_id == nil;
    [self.scrollView addSubview:birthdayView];
    
    YKInputMultilineView* characterView = [[YKInputMultilineView alloc]initWithTitle:@"性格" input:self.detailModel.CHARACTER];
    [self.scrollView addSubview:characterView];
    characterView.editable = self.m_id == nil;
    self.characterView = characterView;
    YKInputMultilineView* hobbyView = [[YKInputMultilineView alloc]initWithTitle:@"爱好" input:self.detailModel.HOBBY];
    [self.scrollView addSubview:hobbyView];
    self.hobbyView = hobbyView;
    hobbyView.editable = self.m_id == nil;
    [self.scrollView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.scrollView);
        make.width.mas_equalTo(self.scrollView.mas_width);
    }];
    
    
    
    [self.scrollView.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@0 leadSpace:8 tailSpace:8];
}


- (void)onSaveButtonTapped:(id)sender {
    NSString *cid = self.selectClientView.selectedRow.id_p;
    NSString *visitno = self.visitno;
    NSString *visitdate = self.visitdate;
    NSString *visitgoal = self.visitPurposeView.input;
    NSString *visitresult = self.visitResultView.input;
    NSString *visitperson = self.visigPersonView.input;
    NSString *linkphone = self.contactView.input;
    NSString *job = self.positionView.input;
    NSString *birthday = self.birthdayView.input;
    NSString *character = self.characterView.input;
    NSString *hobby = self.hobbyView.input;
    
    
    [TFHTTPMANAER POST:@"customerDataVisitLog/doAddCustomerDataVisitLog.htm" parameters:@{@"cid":cid,@"visitno":visitno,@"visitdate":visitdate,@"visitgoal":visitgoal,@"visitresult":visitresult,@"visitperson":visitperson,@"linkphone":linkphone,@"job":job,@"birthday":birthday,@"character":character,@"hobby":hobby} HUD:@"新增拜访中..." success:^(id responseObject) {
        [self showHint:@"新增成功"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(id responseObject, NSError *error) {
        [self showHint:@"新增失败, 请点击重试"];
    }];
    
}
- (void)onCancleButtonTapped:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
